package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.OrderSettingDao;
import com.itheima.dao.OrderStatusDao;
import com.itheima.entity.OrderVo;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryString;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetting;
import com.itheima.pojo.Setmeal;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;


@Service(interfaceClass = OrderStatusService.class)
@Transactional
public class OrderStatusServiceImpl implements OrderStatusService{
    @Autowired
    private OrderStatusDao orderStatusDao;
    @Autowired
    private MemberDao memberDao;
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private OrderSettingDao orderSettingDao;

    @Override
    public PageResult findPage(QueryString queryString) {
        Integer pageSize = queryString.getPageSize();
        Integer currentPage = queryString.getCurrentPage();
        PageHelper.startPage(currentPage,pageSize);
        Page<OrderVo> mapPage = orderStatusDao.findPage(queryString);
        return new PageResult(mapPage.getTotal(),mapPage.getResult());
    }

    @Override
    public List<Setmeal> findAll() {
        return orderStatusDao.findAll();
    }

    @Override
    public Result add(Map map, Integer[] setmealIds) throws Exception {
        //1.判断选择的预约日期是否已经被设置预约
        String orderDate_String = (String) map.get("orderDate");
        Date orderDate = DateUtils.parseString2Date(orderDate_String);
        OrderSetting orderSetting = orderSettingDao.findByOrderDate(orderDate);
        if (orderSetting == null) {
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }

        //2.判断选择的预约日期是否已经预约满
        int number = orderSetting.getNumber();//获取预约日期的设置人数
        int reservations = orderSetting.getReservations();//获取预约日期的已预约人数
        if (reservations >= number) {
            return new Result(false, MessageConstant.ORDER_FULL);
        }

        //3.判断是否为会员，若不是则自动注册为会员
        String phoneNumber = (String) map.get("phoneNumber");
        Member member = memberDao.findByTelephone(phoneNumber);
        Order order = new Order();
        for (Integer setmealId : setmealIds) {
            order.setSetmealId(setmealId);
            if (member == null) {
                member = new Member();
                member.setRegTime(new Date());
                member.setPhoneNumber(phoneNumber);
                member.setName((String) map.get("memberName"));
                memberDao.add(member);
            } else {
                order.setMemberId(member.getId());
                order.setOrderDate(orderDate);
                List<Order> orderList = orderDao.findByCondition(order);
                if (orderList != null && orderList.size() > 0) {//用户在重复预约
                    return new Result(false, MessageConstant.HAS_ORDERED);
                }
            }
            order.setMemberId(member.getId());
            order.setOrderDate(orderDate);
            order.setOrderType((String) map.get("orderType"));
            order.setOrderStatus((String) map.get("orderStatus"));
            orderDao.add(order);
            orderSetting.setReservations(reservations + 1);
            orderSettingDao.editReservationsByOrderDate(orderSetting);
        }
        return new Result(true, MessageConstant.ORDER_SUCCESS);
    }

    //编辑预约根据id查询，数据回显
    @Override
    public OrderVo findById(Integer id) {
        return orderStatusDao.findById(id);
    }

    //编辑预约设置
    @Override
    public void edit(OrderVo orderVo) {

        Integer id = orderVo.getId();
        String memberName = orderVo.getMemberName();
        Date orderDate = orderVo.getOrderDate();
        String orderStatus = orderVo.getOrderStatus();
        String orderType = orderVo.getOrderType();
        Integer memberid=orderStatusDao.findByIdAndMemberId(id);
        orderStatusDao.editmember(memberid,memberName);
        orderStatusDao.editorder(id,orderDate,orderStatus,orderType);
    }

    @Override
    public void delete(Integer id) {
        orderStatusDao.delete(id);
    }

    @Override
    public void addOrderStatus(OrderVo orderVo) {
        orderStatusDao.addOrderStatus(orderVo);
    }

}
